package io.netty.example.mytest.nio.reactor;

import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;

/**
 * @ClassName WriteEventHandler
 * @Description write事件句柄
 * @Author dukezeng
 * @CreatedTime 2019/4/13 18:08
 * @Version 1.0
 **/
public class WriteEventHandler implements EventHandler {
    public void handleEvent(SelectionKey handle) throws Exception {
        System.out.println("===== Write Event Handler =====");

        SocketChannel socketChannel = (SocketChannel) handle.channel();
        byte[] result = (byte[])handle.attachment();
        String  resp = new String(result) + " ack";
        ByteBuffer byteBuffer = ByteBuffer.allocate(20);
        byteBuffer.put(resp.getBytes());
        byteBuffer.flip();
        socketChannel.write(byteBuffer);
        socketChannel.close();
    }
}
